/******************************************************************************
 * * setting_bluetooth_ui.c - implementation of dualsim setting functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/

/*********************
 *      INCLUDES
 *********************/
#include "setting_global.h"

#if USE_LV_BLUETOOTH != 0
/**********************
 *  STATIC PROTOTYPES
 **********************/
static VOID Display_Setting_Bluetooth(VOID);
static VOID Display_Setting_Bluetooth_PairedList(lv_obj_t * Btn, lv_event_t Event);
static VOID Display_Setting_Bluetooth_ScanList(lv_obj_t * Btn, lv_event_t Event);
static VOID Display_Setting_Bluetooth_Scaning(lv_obj_t * Btn, lv_event_t Event);
static VOID Display_Setting_Bluetooth_Edit_Local_Name(lv_obj_t * Btn, lv_event_t Event);
static UINT8 Display_Setting_Bluetooth_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);
static UINT8 Display_Setting_Bluetooth_PairedList_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);
static UINT8 Display_Setting_Bluetooth_Process_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);
static UINT8 Display_Setting_Bluetooth_ScanList_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);
static UINT8 Display_Setting_Bluetooth_Edit_Name_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val);

static VOID Bluetooth_Receive_File_Confirm_Action(lv_obj_t * Mbox, lv_event_t Event);
static VOID Bluetooth_Transfer_File_Finish_Action(lv_obj_t * Mbox, lv_event_t Event);
static VOID Bluetooth_Receive_Abort_Action(lv_obj_t * Mbox, lv_event_t Event);
static NV_UI_Bluetooth_t * Bluetooth_Read_Nvm(VOID);
static VOID Bluetooth_Switch(lv_obj_t * Btn, lv_event_t Event);
static VOID Bluetooth_Process_Btn_Action(lv_obj_t * Btn, lv_event_t Event);
static VOID Bluetooth_Tip_Mbox_Create(lv_obj_t * par);
static VOID Bluetooth_Cancel_Inquiry(VOID);
static VOID Bluetooth_Clean_Scan_List(VOID);
static VOID Bluetooth_Open_State_Init(VOID);
static VOID Bluetooth_Update_Paired_Label_Show(VOID);
static VOID Bluetooth_MainUI_Update(VOID * pPara);
static VOID Bluetooth_Get_Local_Name(INT8 * Name);
static VOID Bluetooth_Save_Local_Name(INT8 * Name, UINT8 Len);
static VOID Bluetooth_Set_BtnBar_Text(VOID);
static VOID Bluetooth_Start_Scaning(VOID);
/**********************
 *  GLOBAL VARIABLES
 **********************/
static UI_Normal_Container_List_Desc_t *gp_BtMainContList = NULL;
static UI_Normal_Container_List_Desc_t *gp_BtPairDevContList = NULL;
static UI_Normal_List_Only_Desc_t *gp_BtScanDevList = NULL;
static UI_Normal_List_Only_Desc_t *gp_BtProcessList = NULL;
static UI_Normal_NoTitle_Label_Ta_Desc_t *gp_BtNameTa = NULL;
static lv_obj_t *gp_OpeningLabel = NULL;
static lv_obj_t *gp_ScaningPreload = NULL;
static lv_obj_t *gp_ProcessMbox = NULL;
static lv_obj_t *gp_TransferMbox = NULL;
/**********************
 *   GLOBAL FUNCTIONS
***********************/
VOID Bluetooth_Start_Without_UI(VOID)
{
    NV_UI_Bluetooth_t * Nvm = Bluetooth_Read_Nvm();
    printf("bt nvm on_off = %d\n", Nvm->on_off);
    if(Nvm->on_off == 1) {
        bt_adp_task_create();
        bt_list_init();
        bt_open_device();
    }
    Hal_Mem_Free(Nvm);
}

lv_obj_t * Bluetooth_Get_Opening_Label(VOID)
{
    return gp_OpeningLabel;
}

VOID Bluetooth_Transfer_File_Finish(bt_file_trans_process Process, UINT8 Success)
{
    Framework_Set_Suspend_Enable(TRUE);
    if(NULL == gp_TransferMbox) return;
    lv_msgbox_ext_t * ext = lv_obj_get_ext_attr(gp_TransferMbox);
    if(ext->btnm) {
        lv_obj_del(ext->btnm);
        ext->btnm = NULL;
    } else if(lv_obj_count_children(gp_TransferMbox) > 1) {
        lv_obj_t * bar = lv_obj_get_child(gp_TransferMbox, NULL);
        lv_obj_del(bar);
    }

    UINT16 txtId = 0;
    if(PROCESS_RECV_FILE == Process) {
        txtId = Success == 1 ? PHONE_TEXT_ID_RECEIVE_FINISHED : PHONE_TEXT_ID_RECEIVE_FAILED;
    } else {
        txtId = Success == 1 ? PHONE_TEXT_ID_SEND_SUCCESS : PHONE_TEXT_ID_SEND_FAILED;
    }
    lv_msgbox_set_text(gp_TransferMbox, (char *)lv_lang_get_text(txtId));

    static const INT8 * FinishTxt[2];
    FinishTxt[0] = (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_OK);
    FinishTxt[1] = "";
    lv_msgbox_add_btns(gp_TransferMbox, FinishTxt);
    lv_obj_set_event_cb(gp_TransferMbox, Bluetooth_Transfer_File_Finish_Action);
    lv_obj_realign(gp_TransferMbox);
    lv_group_add_obj(UI_Normal_Get_Group(), gp_TransferMbox);
    lv_group_focus_obj(gp_TransferMbox);
}

VOID Bluetooth_Receive_File_Abort_Tip(UINT8 Reason)
{
    printf("%s: reason = %d\n", __FUNCTION__,Reason);
    if(!Framework_Get_Lcd_Status()) Framework_Set_Lcd_Status(TRUE);
    lv_obj_t * TipMbox = lv_msgbox_create(FP_PARENT, NULL);
    lv_obj_set_top(TipMbox, TRUE);
    lv_obj_set_width(TipMbox, LV_HOR_RES * 4 / 5);
    if(BT_RECEIVE_NO_SDCARD == Reason)
        lv_msgbox_set_text(TipMbox, (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_NO_SDCARD));
    else
        lv_msgbox_set_text(TipMbox, (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_NO_SPACE));
    static const INT8 * OkTxt[2];
    OkTxt[0] = (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_OK);
    OkTxt[1] = "";
    lv_msgbox_add_btns(TipMbox, OkTxt);
    lv_obj_set_event_cb(TipMbox, Bluetooth_Receive_Abort_Action);
    lv_obj_realign(TipMbox);
}

VOID Bluetooth_Transfer_File_Process_Update(bt_file_trans_process Process)
{
    if(NULL == gp_TransferMbox) return;
    lv_msgbox_ext_t * ext = lv_obj_get_ext_attr(gp_TransferMbox);
    if(ext->btnm) return;
    lv_obj_t * Bar = lv_obj_get_child(gp_TransferMbox, NULL);
    UINT16 Value = 0;
    if(PROCESS_RECV_FILE == Process) {
        bt_file_receive_context * Recv_File = bt_get_receive_file_info();
        Value = Recv_File->file_offset * 100 / Recv_File->file_size;
    } else {
        bt_file_send_context * Send_File = bt_get_send_file_info();
        Value = Send_File->file_offset * 100 / Send_File->file_size;
    }
    printf("%s: value = %d\n", __FUNCTION__, Value);
    lv_bar_set_value(Bar, Value, LV_ANIM_ON);
}

VOID Bluetooth_Receive_File_Confirm(VOID)
{
    if(!Framework_Get_Lcd_Status()) Framework_Set_Lcd_Status(TRUE);
    Framework_Set_Suspend_Enable(FALSE);

    bt_file_receive_context * file_cont = bt_get_receive_file_info();
    if(file_cont == NULL) return;

    if(gp_TransferMbox != NULL) {
        if(UI_Get_User_Num(gp_TransferMbox) == PROCESS_RECV_FILE || UI_Get_User_Num(gp_TransferMbox) == PROCESS_SEND_FILE)
        {
            lv_obj_del(gp_TransferMbox);
        }
        gp_TransferMbox = NULL;
    }
    gp_TransferMbox = lv_msgbox_create(FP_PARENT, NULL);
    lv_obj_set_top(gp_TransferMbox, true);
    lv_obj_set_width(gp_TransferMbox, LV_HOR_RES * 4 / 5);
    char str[MAX_BT_OBEX_FILE_NAME + 20];
    snprintf(str, MAX_BT_OBEX_FILE_NAME + 20, "%s\n%s?", (char *)lv_lang_get_text(PHONE_TEXT_ID_RECEIVE_FILE), file_cont->file_name);
    printf("bt file confirm name= %s\n", file_cont->file_name);
    lv_msgbox_set_text(gp_TransferMbox, str);

    static const char * txt[3];
    txt[0] = (char *)lv_lang_get_text(PHONE_TEXT_ID_OK);
    txt[1] = (char *)lv_lang_get_text(PHONE_TEXT_ID_CANCEL);
    txt[2] = "";
    lv_msgbox_add_btns(gp_TransferMbox, txt);
    lv_obj_set_event_cb(gp_TransferMbox, Bluetooth_Receive_File_Confirm_Action);
    lv_obj_align(gp_TransferMbox, NULL, LV_ALIGN_CENTER, 0, 0);
    UI_Set_User_Num(gp_TransferMbox, PROCESS_RECV_FILE);
    lv_group_add_obj(UI_Normal_Get_Group(), gp_TransferMbox);
    lv_group_focus_obj(gp_TransferMbox);
    // UI_Normal_Set_GroupType_Mbox();
}

VOID Bluetooth_Stop_Scaning_Anim(VOID)
{
    if(Nav_Get_Top() != ACT_ID_BT) return;
    if(gp_ScaningPreload == NULL) return;
    lv_obj_del(gp_ScaningPreload);
    gp_ScaningPreload = NULL;
    lv_obj_set_hidden(lv_obj_get_child(gp_BtMainContList->ContList.ContainerList[4].Label.Label, NULL), false);
    Framework_Set_Suspend_Enable(TRUE);
}

VOID Bluetooth_Update_Display(VOID)
{
    lv_obj_t * dev_list = NULL;
    ACTIVITY_ID ActId = Nav_Get_Top();
    if(gp_ProcessMbox) {
        lv_obj_del(gp_ProcessMbox);
        gp_ProcessMbox = NULL;
    }
    if(ActId == ACT_ID_BT_PAIRED)
        dev_list = gp_BtPairDevContList->List;
    else if(ActId == ACT_ID_BT_SCANED)
        dev_list = gp_BtScanDevList->List.List;
    if(dev_list) {
        if(ActId == ACT_ID_BT_SCANED) {
            Nav_Back(ACT_ID_ANY);
            if(bt_get_scan_dev_cnt() > 0)
                Display_Setting_Bluetooth_ScanList(NULL, LV_EVENT_CLICKED);
        } else if(ActId == ACT_ID_BT_PAIRED) {
            Nav_Back(ACT_ID_ANY);
            if(bt_get_paired_dev_cnt() > 0)
                Display_Setting_Bluetooth_PairedList(NULL, LV_EVENT_CLICKED);
        }
    } else if(ActId == ACT_ID_BT) {
        Bluetooth_Update_Paired_Label_Show();
        Bluetooth_Update_Scan_Label_Show();
    }
}

void Bluetooth_Processing_Mbox_Update(void)
{
    if(Bluetooth_Setting_Is_Top())
    {
        if(gp_ProcessMbox) {
            uint8_t process = UI_Get_User_Num(gp_ProcessMbox);
            if(process == PROCESS_PAIR)
                lv_msgbox_set_text(gp_ProcessMbox, (char *)lv_lang_get_text(PHONE_TEXT_ID_PAIRED_FAILED));
            else if(process == PROCESS_CONNECT)
                lv_msgbox_set_text(gp_ProcessMbox, (char *)lv_lang_get_text(PHONE_TEXT_ID_CONNECT_FAILED));
            else if(process == PROCESS_UNPAIR)
                lv_msgbox_set_text(gp_ProcessMbox, (char *)lv_lang_get_text(PHONE_TEXT_ID_UNPAIR_FAILED));
            else
                lv_msgbox_set_text(gp_ProcessMbox, (char *)lv_lang_get_text(PHONE_TEXT_ID_DISCONNECT_FAILED));
            lv_obj_realign(gp_ProcessMbox);
            lv_msgbox_set_anim_time(gp_ProcessMbox, 0);
            lv_msgbox_start_auto_close(gp_ProcessMbox, 2000);
            gp_ProcessMbox = NULL;
        }
    } else {
        if(gp_TransferMbox) {
            Bluetooth_Transfer_File_Finish(UI_Get_User_Num(gp_TransferMbox), 0);
        }
    }
}

VOID Bluetooth_Nvm_Save_Switch_State(BOOL State)
{
    NV_UI_Bluetooth_t * Nvm = Bluetooth_Read_Nvm();
    Nvm->on_off = State;
    UI_NV_Write_Req(NV_SECTION_UI_BLUETOOTH, 0, sizeof(NV_UI_Bluetooth_t), (UINT8 *)Nvm);
    Hal_Mem_Free(Nvm);
}

BOOL Bluetooth_Setting_Is_Top(VOID)
{
    ACTIVITY_ID ActId = Nav_Get_Top();
    if(ActId >= ACT_ID_BT && ActId <= ACT_ID_BT_PROCESS)
        return TRUE;
    return FALSE;
}

VOID Bluetooth_Show_Object(BOOL State)
{
    if(Nav_Get_Top() != ACT_ID_BT)
       return;
    printf("Bluetooth_Show_Object state = %d\n", State);
    if(State) {
        lv_obj_set_hidden(gp_OpeningLabel, State);
        lv_switch_on(gp_BtMainContList->ContList.ContainerList[0].u.Sw.sw, LV_ANIM_OFF);
        gp_BtMainContList->ContList.ContainerList[0].u.Sw.State = TRUE;
    } else {
        lv_switch_off(gp_BtMainContList->ContList.ContainerList[0].u.Sw.sw, LV_ANIM_OFF);
        gp_BtMainContList->ContList.ContainerList[0].u.Sw.State = FALSE;
    }
    lv_obj_set_hidden(gp_BtMainContList->ContList.ContainerList[2].Button, !State);
    lv_obj_set_hidden(gp_BtMainContList->ContList.ContainerList[3].Button, !State);
    lv_obj_set_hidden(gp_BtMainContList->ContList.ContainerList[4].Button, !State);
    if(State) {
        Bluetooth_Start_Scaning();
    }
    Bluetooth_Set_BtnBar_Text();
}

VOID Bluetooth_Update_Scan_Label_Show(VOID)
{
    if(Nav_Get_Top() != ACT_ID_BT)
       return;
    INT8 Str[20];
    memset(Str, 0, 20);
    if(bt_get_scan_dev_cnt() == 0)
        lv_label_set_text_id(gp_BtMainContList->ContList.ContainerList[3].Label.Label, PHONE_TEXT_ID_AVAILABLE_DEV);
    else {
        snprintf(Str, 20, "%s(%d)", (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_AVAILABLE_DEV), bt_get_scan_dev_cnt());
        lv_label_set_text(gp_BtMainContList->ContList.ContainerList[3].Label.Label, Str);
    }
}

/**
 * Callback Functions
 */
VOID Display_Setting_Bluetooth_Cb(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    Display_Setting_Bluetooth();
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static VOID Display_Setting_Bluetooth_Destroy(VOID *pPara)
{
    UI_Normal_Container_List_Desc_t *NormalContainerListDesc = NULL;

    NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)pPara;

    Hal_Mem_Free(NormalContainerListDesc->ContList.ContainerList);
    Hal_Mem_Free(NormalContainerListDesc);
    gp_BtMainContList = NULL;
    gp_BtPairDevContList = NULL;
    gp_BtProcessList = NULL;
    gp_OpeningLabel = NULL;
    gp_ScaningPreload = NULL;
}

static VOID Display_Setting_Bluetooth_Create(VOID * pPara)
{
    Bluetooth_MainUI_Update(pPara);
    Bluetooth_Show_Object(FALSE);
    Bluetooth_Open_State_Init();
    Bluetooth_Update_Paired_Label_Show();
}

static VOID Display_Setting_Bluetooth_Restore(VOID * pPara)
{
    Bluetooth_MainUI_Update(pPara);
    Bluetooth_Update_Paired_Label_Show();
    Bluetooth_Update_Scan_Label_Show();
}

/**
 * display Setting_Bluetooth
 * param (in) void:
 * return void:
 */
static VOID Display_Setting_Bluetooth(VOID)
{
    UINT8                            ListContainerCnt         = 0;
    UI_Normal_Container_List_Desc_t *NormalContainerListDesc = NULL;
    UI_Container_Base_Desc_t        *ContainerList           = NULL;
    Nav_Func_List_t                 FuncList;

    ListContainerCnt = 5;
    ContainerList    = (UI_Container_Base_Desc_t *)Hal_Mem_Alloc(ListContainerCnt * sizeof(UI_Container_Base_Desc_t));
    Hal_Mem_Set(ContainerList, 0, ListContainerCnt * sizeof(UI_Container_Base_Desc_t));

    NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_Container_List_Desc_t));
    Hal_Mem_Set(NormalContainerListDesc, 0, sizeof(UI_Normal_Container_List_Desc_t));

    NormalContainerListDesc->TitleBar.TxtId      = PHONE_TEXT_ID_BLUETOOTH;
    NormalContainerListDesc->TitleBar.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    NormalContainerListDesc->TitleBar.TxtAlign   = LV_LABEL_ALIGN_CENTER;

    ContainerList[0].Label.TxtId         = PHONE_TEXT_ID_BLUETOOTH;
    ContainerList[0].Label.TxtAlign      = LV_LABEL_ALIGN_LEFT;
    ContainerList[0].Label.LabelAlign    = LV_ALIGN_IN_LEFT_MID;
    ContainerList[0].Valid               = TRUE;
    ContainerList[0].Choise              = UI_BASE_OBJ_SW_ONE_ROW;
    ContainerList[0].u.Sw.State          = FALSE;
    ContainerList[0].ReleaseFunc         = Bluetooth_Switch;

    ContainerList[1].Label.TxtId      = PHONE_TEXT_ID_DEVICE_NAME;
    ContainerList[1].Label.TxtAlign   = LV_LABEL_ALIGN_LEFT;
    ContainerList[1].Label.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    ContainerList[1].Valid            = TRUE;
    ContainerList[1].Choise           = UI_BASE_OBJ_LABEL;
    ContainerList[1].u.Label2.TxtAlign = LV_LABEL_ALIGN_RIGHT;
    ContainerList[1].ReleaseFunc       = Display_Setting_Bluetooth_Edit_Local_Name;

    ContainerList[2].Label.TxtId         = PHONE_TEXT_ID_PAIRED_DEV;
    ContainerList[2].Label.TxtAlign      = LV_LABEL_ALIGN_LEFT;
    ContainerList[2].Label.LabelAlign    = LV_ALIGN_IN_TOP_LEFT;
    ContainerList[2].Valid               = TRUE;
    ContainerList[2].Choise              = UI_BASE_OBJ_NULL;
    ContainerList[2].ReleaseFunc         = Display_Setting_Bluetooth_PairedList;

    ContainerList[3].Label.TxtId         = PHONE_TEXT_ID_AVAILABLE_DEV;
    ContainerList[3].Label.TxtAlign      = LV_LABEL_ALIGN_LEFT;
    ContainerList[3].Label.LabelAlign    = LV_ALIGN_IN_TOP_LEFT;
    ContainerList[3].Valid               = TRUE;
    ContainerList[3].Choise              = UI_BASE_OBJ_NULL;
    ContainerList[3].ReleaseFunc         = Display_Setting_Bluetooth_ScanList;

    ContainerList[4].Label.TxtId         = PHONE_TEXT_ID_REFRESH;
    ContainerList[4].Label.TxtAlign      = LV_LABEL_ALIGN_LEFT;
    ContainerList[4].Label.LabelAlign    = LV_ALIGN_IN_TOP_LEFT;
    ContainerList[4].Valid               = TRUE;
    ContainerList[4].Choise              = UI_BASE_OBJ_NULL;
    ContainerList[4].ReleaseFunc         = Display_Setting_Bluetooth_Scaning;

    NormalContainerListDesc->ContList.ListContainerCnt = ListContainerCnt;
    NormalContainerListDesc->ContList.Index            = 0;
    NormalContainerListDesc->ContList.ContainerList    = ContainerList;

    NormalContainerListDesc->ButtonBar.ButtonL.Valid = FALSE;

    NormalContainerListDesc->ButtonBar.ButtonM.Valid = TRUE;
    NormalContainerListDesc->ButtonBar.ButtonM.TxtId = PHONE_TEXT_ID_SELECT;

    NormalContainerListDesc->ButtonBar.ButtonR.Valid = TRUE;
    NormalContainerListDesc->ButtonBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;

    NormalContainerListDesc->KeyFunc = Display_Setting_Bluetooth_KeyCb;

    Hal_Mem_Set(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow    = UI_Normal_Container_List_Create;
    FuncList.OnDestroy = Display_Setting_Bluetooth_Destroy;
    FuncList.OnCreate = Display_Setting_Bluetooth_Create;
    FuncList.OnRestoreState = Display_Setting_Bluetooth_Restore;

    gp_BtMainContList = NormalContainerListDesc;
    Nav_Forward(ACT_ID_ANY, ACT_ID_BT, &FuncList, (VOID *)NormalContainerListDesc);
}

static VOID Display_Setting_Bluetooth_Process_Destroy(VOID *pPara)
{
    UI_Normal_List_Only_Desc_t *Desc = pPara;
    Hal_Mem_Free(Desc->List.ButtonList);
    Hal_Mem_Free(Desc);
    gp_BtProcessList = NULL;
}

static VOID Display_Setting_Bluetooth_Process_Option(bt_paired_dev_t * Dev)
{
    Nav_Func_List_t            FuncList;
    UI_Normal_List_Only_Desc_t *pDesc = NULL;
    UINT32                     Size   = 0;
    UINT8                      i      = 0;

    FuncList.OnShow         = (Void_P_Func_t)UI_Normal_List_Only_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = NULL;
    FuncList.OnDestroy      = Display_Setting_Bluetooth_Process_Destroy;
    FuncList.OnSaveState    = NULL;
    FuncList.OnRestoreState = NULL;

    pDesc = (UI_Normal_List_Only_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_List_Only_Desc_t));
    Hal_Mem_Set(pDesc, 0, sizeof(UI_Normal_List_Only_Desc_t));

    pDesc->TitleBar.TxtId    = LV_LANG_TXT_ID_NONE;
    pDesc->TitleBar.TxtAlign = LV_LABEL_ALIGN_CENTER;

    if((Dev->cod & COD_TYPE_HEADSET) == COD_TYPE_HEADSET)
        pDesc->List.ListButtonCnt = 2;
    else
        pDesc->List.ListButtonCnt = 1;
    Size                      = sizeof(UI_Button_Desc_t) * pDesc->List.ListButtonCnt;
    pDesc->List.ButtonList    = (UI_Button_Desc_t *)Hal_Mem_Alloc(Size);
    Hal_Mem_Set(pDesc->List.ButtonList, 0, Size);

    if(pDesc->List.ListButtonCnt == 2)
    {
        if(Dev->profile_status[PROFILE_CONNECTION] == PROFILE_STATE_CONNECTED)
        {
            pDesc->List.ButtonList[i].Valid        = TRUE;
            pDesc->List.ButtonList[i].ButtonIndex  = i;
            pDesc->List.ButtonList[i].TxtId        = PHONE_TEXT_ID_DISCONNECT;
            pDesc->List.ButtonList[i++].ReleaseFun = Bluetooth_Process_Btn_Action;
        }
        else
        {
            pDesc->List.ButtonList[i].Valid        = TRUE;
            pDesc->List.ButtonList[i].ButtonIndex  = i;
            pDesc->List.ButtonList[i].TxtId        = PHONE_TEXT_ID_CONNECT;
            pDesc->List.ButtonList[i++].ReleaseFun = Bluetooth_Process_Btn_Action;
        }
        pDesc->List.ButtonList[i].Valid        = TRUE;
        pDesc->List.ButtonList[i].ButtonIndex  = i;
        pDesc->List.ButtonList[i].TxtId        = PHONE_TEXT_ID_UNPAIRED;
        pDesc->List.ButtonList[i++].ReleaseFun = Bluetooth_Process_Btn_Action;
    }
    else
    {
        pDesc->List.ButtonList[i].Valid        = TRUE;
        pDesc->List.ButtonList[i].ButtonIndex  = i;
        pDesc->List.ButtonList[i].TxtId        = PHONE_TEXT_ID_UNPAIRED;
        pDesc->List.ButtonList[i++].ReleaseFun = Bluetooth_Process_Btn_Action;
    }

    pDesc->BtnBar.ButtonM.Valid = TRUE;
    pDesc->BtnBar.ButtonM.TxtId = PHONE_TEXT_ID_SELECT;
    pDesc->BtnBar.ButtonR.Valid = TRUE;
    pDesc->BtnBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;
    pDesc->KeyFunc              = Display_Setting_Bluetooth_Process_KeyCb;

    gp_BtProcessList = pDesc;
    Nav_Forward(ACT_ID_ANY, ACT_ID_BT_PROCESS, &FuncList, pDesc);
    lv_label_set_text(pDesc->TitleBar.Label,(INT8 *)Dev->name);
    for(i = 0; i < pDesc->List.ListButtonCnt; i++)
    {
        UI_Set_User_Ptr(pDesc->List.ButtonList[i].Button, (VOID*)Dev);
        UI_Set_User_Num(pDesc->List.ButtonList[i].Button, pDesc->List.ButtonList[i].TxtId);
    }
}

static lv_obj_t * Bluetooth_Processing_Mbox_Create(INT8 * name, UINT8 process)
{
    INT8 str[64];
    lv_obj_t * mbox = lv_msgbox_create(UI_Normal_Get_Current_Win(), NULL);
    memset(str, 0, 64);
    if(process == PROCESS_PAIR)
        snprintf(str, 64, "%s\n%s", (char *)lv_lang_get_text(PHONE_TEXT_ID_PAIRING), name);
    else if(process == PROCESS_CONNECT)
        snprintf(str, 64, "%s\n%s", (char *)lv_lang_get_text(PHONE_TEXT_ID_CONNECTION), name);
    else if(process == PROCESS_UNPAIR)
        snprintf(str, 64, "%s\n%s", (char *)lv_lang_get_text(PHONE_TEXT_ID_UNPAIRING), name);
    else if(process == PROCESS_DISCONNECT)
        snprintf(str, 64, "%s\n%s", (char *)lv_lang_get_text(PHONE_TEXT_ID_DISCONNECTING), name);
    lv_msgbox_set_text(mbox, str);
    lv_obj_set_size(mbox, LV_HOR_RES * 2 / 3, LV_VER_RES * 2 / 3);
    lv_obj_align(mbox, NULL, LV_ALIGN_CENTER, 0, 0);
    UI_Set_User_Num(mbox, process);
    gp_ProcessMbox = mbox;
    return mbox;
}
static VOID Bluetooth_Process_Btn_Action(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    UINT16 txtid = UI_Get_User_Num(Btn);
    bt_paired_dev_t * dev = (bt_paired_dev_t *) UI_Get_User_Ptr(Btn);
    struct bt_addr addr;
    INT32 ret = 0;
    memcpy(addr.bytes, dev->addr, 6);
    Nav_Back(ACT_ID_ANY);
    if(PHONE_TEXT_ID_UNPAIRED == txtid)
    {
        Bluetooth_Processing_Mbox_Create((INT8 *)dev->name, PROCESS_UNPAIR);
        ret = bt_remove_device(addr, dev->cod);
    }
    else if(PHONE_TEXT_ID_CONNECT == txtid)
    {
        if(bt_get_headset_connect() || gp_TransferMbox != NULL)
        {
            Bluetooth_Tip_Mbox_Create(UI_Normal_Get_Current_Win());
            return;
        }
        Bluetooth_Processing_Mbox_Create((INT8 *)dev->name, PROCESS_CONNECT);
        ret = bt_connect_device(addr, dev->cod);
    }
    else
    {
        Bluetooth_Processing_Mbox_Create((INT8 *)dev->name, PROCESS_DISCONNECT);
        ret = bt_disconnect_device(addr, dev->cod);
    }
    if(-1 == ret)
    {
        Bluetooth_Processing_Mbox_Update();
    }
}

static VOID Bluetooth_Paired_List_Btn_Action(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    bt_paired_dev_t * dev = (bt_paired_dev_t *)UI_Get_User_Ptr(Btn);
    Display_Setting_Bluetooth_Process_Option(dev);
}

static VOID Bluetooth_Scaned_List_Btn_Action(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    if(bt_get_headset_connect() || gp_TransferMbox != NULL) {
        Bluetooth_Tip_Mbox_Create(UI_Normal_Get_Current_Win());
        return;
    }

    struct bt_addr addr;
    uint8_t index = UI_Get_User_Num(Btn);
    struct bt_event_inquiry * scan_dev = bt_get_scan_dev(index);
    printf("bt find bounding node cod = %x\n\n", scan_dev->cod);

    memcpy(addr.bytes, scan_dev->addr, 6);
    bt_bonding_device(addr, scan_dev->cod);

    lv_obj_t * mbox = Bluetooth_Processing_Mbox_Create((char *)scan_dev->name, PROCESS_PAIR);
    UI_Set_User_Ptr(mbox, scan_dev);
}

static VOID Display_Setting_Bluetooth_Edit_Local_Name_Create(VOID *pPara)
{
    UI_Normal_NoTitle_Label_Ta_Desc_t *NormalNoTitleLblTaDesc = NULL;

    NormalNoTitleLblTaDesc = (UI_Normal_NoTitle_Label_Ta_Desc_t *)pPara;
    lv_label_set_text(NormalNoTitleLblTaDesc->ContLabelTr.Label, "");

    // set max length of TA
    lv_textarea_set_max_length(NormalNoTitleLblTaDesc->ContTa.TextArea, NV_BLUETOOTH_DEV_NAME_LEN - 1);
}
static VOID Display_Setting_Bluetooth_Edit_Local_Name_Destroy(VOID *pPara)
{
    UI_Normal_NoTitle_Label_Ta_Desc_t *NormalNoTitleLblTaDesc = NULL;
    NormalNoTitleLblTaDesc = (UI_Normal_NoTitle_Label_Ta_Desc_t *)pPara;
    Hal_Mem_Free(NormalNoTitleLblTaDesc->ContTa.TaTxt);
    Hal_Mem_Free(NormalNoTitleLblTaDesc);
}

static VOID Display_Setting_Bluetooth_Edit_Local_Name_RestoreState(VOID *pPara)
{
    gp_BtNameTa = (VOID *)pPara;
}

static VOID Display_Setting_Bluetooth_Edit_Local_Name(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    if(!bt_get_state()) return;
    UI_Normal_NoTitle_Label_Ta_Desc_t *NormalNoTitleLblTaDesc = NULL;
    Nav_Func_List_t                   FuncList;

    NormalNoTitleLblTaDesc = (UI_Normal_NoTitle_Label_Ta_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_NoTitle_Label_Ta_Desc_t));
    Hal_Mem_Set(NormalNoTitleLblTaDesc, 0, sizeof(UI_Normal_NoTitle_Label_Ta_Desc_t));
    NormalNoTitleLblTaDesc->ContLabelTr.TxtAlign = LV_LABEL_ALIGN_RIGHT;
    NormalNoTitleLblTaDesc->ContLabel.TxtId    = PHONE_TEXT_ID_DEVICE_NAME;
    NormalNoTitleLblTaDesc->ContLabel.TxtAlign = LV_LABEL_ALIGN_LEFT;
    NormalNoTitleLblTaDesc->ContTa.TaTxt       = (INT8 *)Hal_Mem_Alloc(NV_BLUETOOTH_DEV_NAME_LEN);
    Bluetooth_Get_Local_Name(NormalNoTitleLblTaDesc->ContTa.TaTxt);
    NormalNoTitleLblTaDesc->ContTa.TaCursorHide = FALSE;
    NormalNoTitleLblTaDesc->ContTa.TaAlign      = LV_LABEL_ALIGN_LEFT;

    NormalNoTitleLblTaDesc->ButtonCont.ButtonL.Valid = TRUE;
    NormalNoTitleLblTaDesc->ButtonCont.ButtonL.TxtId = PHONE_TEXT_ID_BACK;
    NormalNoTitleLblTaDesc->ButtonCont.ButtonM.Valid = TRUE;
    NormalNoTitleLblTaDesc->ButtonCont.ButtonM.TxtId = PHONE_TEXT_ID_OK;
    NormalNoTitleLblTaDesc->ButtonCont.ButtonR.Valid = TRUE;
    NormalNoTitleLblTaDesc->ButtonCont.ButtonR.TxtId = PHONE_TEXT_ID_CLEAR;

    NormalNoTitleLblTaDesc->KeyFunc = Display_Setting_Bluetooth_Edit_Name_KeyCb;

    Hal_Mem_Set(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow         = UI_Normal_NoTitle_Label_Ta_Create;
    FuncList.OnCreate       = Display_Setting_Bluetooth_Edit_Local_Name_Create;
    FuncList.OnDestroy      = Display_Setting_Bluetooth_Edit_Local_Name_Destroy;
    FuncList.OnRestoreState = Display_Setting_Bluetooth_Edit_Local_Name_RestoreState;

    gp_BtNameTa = NormalNoTitleLblTaDesc;
    Nav_Forward(ACT_ID_BT, ACT_ID_BT_NAMEEDIT, &FuncList, (VOID *)NormalNoTitleLblTaDesc);
}

static VOID Display_Setting_Bluetooth_PairedList_Destroy(VOID *pPara)
{
    UI_Normal_Container_List_Desc_t *NormalContainerListDesc = NULL;

    NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)pPara;

    Hal_Mem_Free(NormalContainerListDesc->ContList.ContainerList);
    Hal_Mem_Free(NormalContainerListDesc);
    gp_BtPairDevContList = NULL;
    gp_ProcessMbox = NULL;
}

static VOID Display_Setting_Bluetooth_PairedList_Create(VOID *pPara)
{
    Bluetooth_Cancel_Inquiry();
    UI_Normal_Container_List_Desc_t *NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)pPara;
    UINT8 i = 0;
    INT8 name_str[MAX_BT_NAME];
    bt_paired_dev_t * paired_list = bt_get_paired_list();
    for(i = 0; i < NormalContainerListDesc->ContList.ListContainerCnt; i++)
    {
        memset(name_str, 0, MAX_BT_NAME);
        if(paired_list[i].name[0] == NULL)
        {
            snprintf(name_str, MAX_BT_NAME, "%2x:%2x:%2x:%2x:%2x:%2x", paired_list[i].addr[0], paired_list[i].addr[1], paired_list[i].addr[2],
                     paired_list[i].addr[3], paired_list[i].addr[4], paired_list[i].addr[5]);
        }
        else
        {
            snprintf(name_str, MAX_BT_NAME, "%s", paired_list[i].name);
            lv_label_set_text(NormalContainerListDesc->ContList.ContainerList[i].Label.Label, name_str);
            if(paired_list[i].profile_status[PROFILE_CONNECTION] == PROFILE_STATE_CONNECTED
               && (paired_list[i].cod & COD_TYPE_HEADSET) == COD_TYPE_HEADSET)
            {
                lv_label_set_text_id(NormalContainerListDesc->ContList.ContainerList[i].u.Label2.Label, PHONE_TEXT_ID_CONNECTED);
            }
            else
            {
                lv_label_set_text(NormalContainerListDesc->ContList.ContainerList[i].u.Label2.Label, "");
            }
            if(paired_list[i].profile_status[PROFILE_CONNECTION] != PROFILE_STATE_MAX)
            {
                lv_obj_t * right_label = lv_label_create(NormalContainerListDesc->ContList.ContainerList[i].Label.Label, NULL);
                lv_label_set_text(right_label, LV_SYMBOL_RIGHT);
                lv_obj_align(right_label, NULL, LV_ALIGN_IN_RIGHT_MID, -10, 0);
            }
            UI_Set_User_Ptr(NormalContainerListDesc->ContList.ContainerList[i].Button, &paired_list[i]);
        }
        printf("bt pairedlist create name=%s\n", name_str);
    }
}

static VOID Display_Setting_Bluetooth_PairedList(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    if(bt_get_paired_dev_cnt() == 0) return;
    UINT8                            ListContainerCnt         = 0;
    UI_Normal_Container_List_Desc_t *NormalContainerListDesc = NULL;
    UI_Container_Base_Desc_t        *ContainerList           = NULL;
    Nav_Func_List_t                 FuncList;
    UINT8                           i                        = 0;

    ListContainerCnt = bt_get_paired_dev_cnt();
    ContainerList    = (UI_Container_Base_Desc_t *)Hal_Mem_Alloc(ListContainerCnt * sizeof(UI_Container_Base_Desc_t));
    Hal_Mem_Set(ContainerList, 0, ListContainerCnt * sizeof(UI_Container_Base_Desc_t));
    NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_Container_List_Desc_t));
    Hal_Mem_Set(NormalContainerListDesc, 0, sizeof(UI_Normal_Container_List_Desc_t));

    NormalContainerListDesc->TitleBar.TxtId      = PHONE_TEXT_ID_PAIRED_DEV;
    NormalContainerListDesc->TitleBar.LabelAlign = LV_ALIGN_IN_TOP_LEFT;
    NormalContainerListDesc->TitleBar.TxtAlign   = LV_LABEL_ALIGN_CENTER;

    for(i = 0; i < ListContainerCnt; i++)
    {
        ContainerList[i].Label.TxtId         = LV_LANG_TXT_ID_NONE;
        ContainerList[i].Label.TxtAlign      = LV_LABEL_ALIGN_LEFT;
        ContainerList[i].Label.LabelAlign    = LV_ALIGN_IN_LEFT_MID;
        ContainerList[i].Valid               = TRUE;
        ContainerList[i].Choise              = UI_BASE_OBJ_LABEL;
        ContainerList[i].ReleaseFunc         = Bluetooth_Paired_List_Btn_Action;
        ContainerList[i].u.Label2.TxtId      = LV_LANG_TXT_ID_NONE;
        ContainerList[i].u.Label2.TxtAlign   = LV_LABEL_ALIGN_RIGHT;
    }

    NormalContainerListDesc->ContList.ListContainerCnt = ListContainerCnt;
    NormalContainerListDesc->ContList.Index            = 0;
    NormalContainerListDesc->ContList.ContainerList    = ContainerList;

    NormalContainerListDesc->ButtonBar.ButtonL.Valid = FALSE;

    NormalContainerListDesc->ButtonBar.ButtonM.Valid = TRUE;
    NormalContainerListDesc->ButtonBar.ButtonM.TxtId = PHONE_TEXT_ID_SELECT;

    NormalContainerListDesc->ButtonBar.ButtonR.Valid = TRUE;
    NormalContainerListDesc->ButtonBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;

    NormalContainerListDesc->KeyFunc = Display_Setting_Bluetooth_PairedList_KeyCb;

    Hal_Mem_Set(&FuncList, 0, sizeof(Nav_Func_List_t));
    FuncList.OnShow         = (Void_P_Func_t)UI_Normal_Container_List_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = Display_Setting_Bluetooth_PairedList_Create;
    FuncList.OnDestroy      = Display_Setting_Bluetooth_PairedList_Destroy;
    FuncList.OnSaveState    = NULL;
    FuncList.OnRestoreState = Display_Setting_Bluetooth_PairedList_Create;

    gp_BtPairDevContList = NormalContainerListDesc;
    Nav_Forward(ACT_ID_ANY, ACT_ID_BT_PAIRED, &FuncList, NormalContainerListDesc);
}

static VOID Display_Setting_Bluetooth_ScanList_Destroy(VOID *pPara)
{
    UI_Normal_List_Only_Desc_t *Desc = pPara;
    Hal_Mem_Free(Desc->List.ButtonList);
    Hal_Mem_Free(Desc);
    gp_BtScanDevList = NULL;
    gp_ProcessMbox = NULL;
}

static VOID Display_Setting_Bluetooth_ScanList(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    if(bt_get_scan_dev_cnt() == 0) return ;
    Nav_Func_List_t            FuncList;
    UI_Normal_List_Only_Desc_t *pDesc = NULL;
    UINT32                     Size   = 0;
    UINT8                      i      = 0;

    FuncList.OnShow         = (Void_P_Func_t)UI_Normal_List_Only_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = NULL;
    FuncList.OnDestroy      = Display_Setting_Bluetooth_ScanList_Destroy;
    FuncList.OnSaveState    = NULL;
    FuncList.OnRestoreState = NULL;

    pDesc = (UI_Normal_List_Only_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Normal_List_Only_Desc_t));
    Hal_Mem_Set(pDesc, 0, sizeof(UI_Normal_List_Only_Desc_t));

    pDesc->TitleBar.TxtId    = PHONE_TEXT_ID_AVAILABLE_DEV;
    pDesc->TitleBar.TxtAlign = LV_LABEL_ALIGN_CENTER;

    pDesc->List.ListButtonCnt = bt_get_scan_dev_cnt();
    Size                      = sizeof(UI_Button_Desc_t) * pDesc->List.ListButtonCnt;
    pDesc->List.ButtonList    = (UI_Button_Desc_t *)Hal_Mem_Alloc(Size);
    Hal_Mem_Set(pDesc->List.ButtonList, 0, Size);

    struct bt_event_inquiry * scan_list = bt_get_scan_list();
    for(i = 0; i < pDesc->List.ListButtonCnt; i++)
    {
        pDesc->List.ButtonList[i].Valid        = TRUE;
        pDesc->List.ButtonList[i].ButtonIndex  = i;
        pDesc->List.ButtonList[i].TxtId        = LV_LANG_TXT_ID_NONE;
        pDesc->List.ButtonList[i].BtnTxt       = scan_list[i].name;
        pDesc->List.ButtonList[i].ReleaseFun = Bluetooth_Scaned_List_Btn_Action;
    }

    pDesc->BtnBar.ButtonM.Valid = TRUE;
    pDesc->BtnBar.ButtonM.TxtId = PHONE_TEXT_ID_SELECT;
    pDesc->BtnBar.ButtonR.Valid = TRUE;
    pDesc->BtnBar.ButtonR.TxtId = PHONE_TEXT_ID_BACK;
    pDesc->KeyFunc              = Display_Setting_Bluetooth_ScanList_KeyCb;

    gp_BtScanDevList = pDesc;
    Bluetooth_Cancel_Inquiry();
    Nav_Forward(ACT_ID_ANY, ACT_ID_BT_SCANED, &FuncList, pDesc);
    for(i = 0; i < pDesc->List.ListButtonCnt; i++)
    {
        UI_Set_User_Num(pDesc->List.ButtonList[i].Button, i);
    }
}

static VOID Display_Setting_Bluetooth_Scaning(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    lv_obj_t * Btnlabel = lv_obj_get_child(gp_BtMainContList->ButtonBar.ButtonM.Button, NULL);
    UINT8 state = bt_get_inquiry_state();
    if(state == BT_INQUIRY_INQUIRYING)
    {
        Bluetooth_Cancel_Inquiry();
        Bluetooth_Stop_Scaning_Anim();
        lv_label_set_text_id(Btnlabel, PHONE_TEXT_ID_REFRESH);
    }
    else
    {
        Bluetooth_Clean_Scan_List();
        Framework_Set_Suspend_Enable(FALSE);
        int8_t ret = bt_inquiry_device();
        if(ret == 0) {
             lv_obj_t * label = gp_BtMainContList->ContList.ContainerList[4].Label.Label;
             lv_obj_set_hidden(lv_obj_get_child(label, NULL), true);

             lv_obj_t * preload = lv_spinner_create(label, NULL);
             lv_obj_set_size(preload, 30, 30);
             lv_obj_set_style_local_bg_opa(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, LV_OPA_0);
             lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_WHITE);
             lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 2);
             lv_obj_set_style_local_line_rounded(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 1);
             lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GRAY);
             lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, 2);
             lv_spinner_set_type(preload, LV_SPINNER_TYPE_SPINNING_ARC);
             lv_spinner_set_spin_time(preload, 3000);
             lv_obj_align(preload, NULL, LV_ALIGN_IN_RIGHT_MID, -5, 0);
             gp_ScaningPreload = preload;
             lv_label_set_text_id(Btnlabel, PHONE_TEXT_ID_STOP);
        }
    }
}

static UINT8 Display_Setting_Bluetooth_Edit_Name_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
    INT8                              *Text                   = NULL;
    UINT8                             Len                     = 0;

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    switch (key_val)
    {
    case KEY_OK:
        Text = (INT8 *)lv_textarea_get_text(gp_BtNameTa->ContTa.TextArea);
        Len  = strlen(Text);
        if (0 == strlen(Text))
        {
            Display_Setting_Error(ACT_ID_BT_NAMEEDIT, PHONE_TEXT_ID_EMPTY, ACT_ID_BT_NAMEEDIT, TRUE);
        }
        else
        {
            Len = strlen(Text) + 1;
            if(bt_change_local_devname(Text) == 0)
                Bluetooth_Save_Local_Name(Text, Len);
            else
                Display_Setting_Error(ACT_ID_BT_NAMEEDIT, PHONE_TEXT_ID_EMPTY, ACT_ID_BT_NAMEEDIT, TRUE);
           Nav_Back(ACT_ID_ANY);
        }
        return(1);

    case KEY_SOFTLEFT:
        Nav_Back(ACT_ID_ANY);
        return(1);

    case KEY_END:
        Nav_Home(ACT_ID_ANY);
        return(1);

    default:
        return(0);
    }
}

static UINT8 Display_Setting_Bluetooth_ScanList_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
     UI_Normal_List_Only_Desc_t *ListDesc = (UI_Normal_List_Only_Desc_t *)gp_BtScanDevList;

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    if (KEY_DOWN == key_val)
    {
        ListDesc->List.Index++;
        ListDesc->List.Index %= ListDesc->List.ListButtonCnt;
        return(0);
    }
    else if (KEY_UP == key_val)
    {
        ListDesc->List.Index += ListDesc->List.ListButtonCnt;
        ListDesc->List.Index--;
        ListDesc->List.Index %= ListDesc->List.ListButtonCnt;
        return(0);
    }
    else if (KEY_SOFTRIGHT == key_val)
    {
        // return to previous UI
        Nav_Back(ACT_ID_ANY);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }
}

static UINT8 Display_Setting_Bluetooth_Process_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
     UI_Normal_List_Only_Desc_t *ListDesc = (UI_Normal_List_Only_Desc_t *)gp_BtProcessList;

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    if (KEY_DOWN == key_val)
    {
        ListDesc->List.Index++;
        ListDesc->List.Index %= ListDesc->List.ListButtonCnt;
        return(0);
    }
    else if (KEY_UP == key_val)
    {
        ListDesc->List.Index += ListDesc->List.ListButtonCnt;
        ListDesc->List.Index--;
        ListDesc->List.Index %= ListDesc->List.ListButtonCnt;
        return(0);
    }
    else if (KEY_SOFTRIGHT == key_val)
    {
        // return to previous UI
        Nav_Back(ACT_ID_ANY);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }
}

static UINT8 Display_Setting_Bluetooth_PairedList_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
     UI_Normal_Container_List_Desc_t *NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)gp_BtPairDevContList;

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    if (KEY_DOWN == key_val)
    {
        NormalContainerListDesc->ContList.Index++;
        NormalContainerListDesc->ContList.Index %= NormalContainerListDesc->ContList.ListContainerCnt;
        return(0);
    }
    else if (KEY_UP == key_val)
    {
        NormalContainerListDesc->ContList.Index += NormalContainerListDesc->ContList.ListContainerCnt;
        NormalContainerListDesc->ContList.Index--;
        NormalContainerListDesc->ContList.Index %= NormalContainerListDesc->ContList.ListContainerCnt;
        return(0);
    }
    else if (KEY_SOFTRIGHT == key_val)
    {
        // return to previous UI
        Nav_Back(ACT_ID_ANY);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }
}

static UINT8 Display_Setting_Bluetooth_KeyCb(lv_obj_t *obj, UI_KEY_STATUS key_sta, UI_KEY_VALUE key_val)
{
     UI_Normal_Container_List_Desc_t *NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)gp_BtMainContList;
     lv_obj_t * sw = NormalContainerListDesc->ContList.ContainerList[0].u.Sw.sw;
     lv_obj_t * label = lv_obj_get_child(NormalContainerListDesc->ButtonBar.ButtonM.Button, NULL);
     UINT8 ShowItemCnt = 0;
     if(lv_switch_get_state(sw))
     {
         ShowItemCnt = NormalContainerListDesc->ContList.ListContainerCnt;
     }
     else
     {
         ShowItemCnt = 2;
     }

    if (KEY_RELEASED != key_sta)
    {
        return(0);
    }

    if (KEY_DOWN == key_val)
    {
        NormalContainerListDesc->ContList.Index++;
        NormalContainerListDesc->ContList.Index %= ShowItemCnt;
        Bluetooth_Set_BtnBar_Text();
        return(0);
    }
    else if (KEY_UP == key_val)
    {
        NormalContainerListDesc->ContList.Index += ShowItemCnt;
        NormalContainerListDesc->ContList.Index--;
        NormalContainerListDesc->ContList.Index %= ShowItemCnt;
        Bluetooth_Set_BtnBar_Text();
        return(0);
    }
    else if (KEY_SOFTRIGHT == key_val)
    {
        // return to previous UI
        Nav_Back(ACT_ID_ANY);
        return(1);
    }
    else if (KEY_END == key_val)
    {
        Nav_Home(ACT_ID_ANY);
        return(1);
    }
    else
    {
        return(0);
    }

}

static VOID Bluetooth_Cancel_Inquiry(VOID)
{
    if(BT_INQUIRY_INQUIRYING == bt_get_inquiry_state()) {
        bt_inquiry_device_cancel();
    }
    Framework_Set_Suspend_Enable(TRUE);
}

static VOID Bluetooth_Clean_Scan_List(VOID)
{
    bt_clean_scan_list();
    Bluetooth_Update_Scan_Label_Show();
}

static VOID Bluetooth_Switch(lv_obj_t * Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    lv_obj_t * sw = gp_BtMainContList->ContList.ContainerList[0].u.Sw.sw;
    if(lv_switch_get_state(sw) == 0)
    {
        bt_adp_task_create();
        bt_list_init();
        if(lv_obj_get_hidden(gp_OpeningLabel))
                lv_obj_set_hidden(gp_OpeningLabel, FALSE);
        lv_label_set_text_id(gp_OpeningLabel, PHONE_TEXT_ID_OPENING_BT);
        lv_refr_now(NULL);
        INT32 ret = bt_open_device();
        if(ret != 0)
        {
            lv_label_set_text_id(gp_OpeningLabel, PHONE_TEXT_ID_OPEN_FAILED);
            lv_obj_realign(gp_OpeningLabel);
            bt_list_destroy();
            bt_adp_task_destroy();
        }
    }
    else
    {
        bt_close_device();
        Bluetooth_Clean_Scan_List();
        Bluetooth_Show_Object(FALSE);
        lv_label_set_text_id(gp_OpeningLabel, PHONE_TEXT_ID_CLOSING);
        lv_obj_set_hidden(gp_OpeningLabel, FALSE);
        Framework_Set_Suspend_Enable(TRUE);
    }
}

static VOID Bluetooth_Receive_Abort_Action(lv_obj_t * Mbox, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    lv_obj_del(lv_obj_get_parent(Mbox));
}

static VOID Bluetooth_Transfer_File_Finish_Action(lv_obj_t * Mbox, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    lv_obj_del(gp_TransferMbox);
    gp_TransferMbox = NULL;
    //UI_Normal_Recover_GroupType();
}

static VOID Bluetooth_Receive_File_Confirm_Action(lv_obj_t * Mbox, lv_event_t Event)
{
    if(LV_EVENT_CLICKED != Event) return;
    INT8* Txt = (INT8 *)lv_msgbox_get_active_btn_text(Mbox);
    if(strcmp(Txt, (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_OK)) == 0) {
        lv_msgbox_set_text(gp_TransferMbox, (INT8 *)lv_lang_get_text(PHONE_TEXT_ID_RECEIVING_FILE));
        lv_msgbox_ext_t * ext = lv_obj_get_ext_attr(gp_TransferMbox);
        lv_obj_del(ext->btnm);
        ext->btnm = NULL;

        lv_obj_t * bar = lv_bar_create(gp_TransferMbox, NULL);
        lv_obj_set_style_local_bg_color(bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x00, 0x99, 0xFF));
        lv_obj_set_style_local_bg_grad_color(bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x15, 0x68, 0xD2));
        lv_obj_set_size(bar, lv_obj_get_width(gp_TransferMbox) - 8, 20);
        lv_obj_align(bar, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -5);
        lv_bar_set_value(bar, 0, LV_ANIM_OFF);
        lv_obj_realign(gp_TransferMbox);
        bt_file_receive_continue();
    } else {
        bt_file_receive_cancel();
        lv_obj_del(gp_TransferMbox);
        gp_TransferMbox = NULL;
        //UI_Normal_Recover_GroupType();
        Framework_Set_Suspend_Enable(TRUE);
    }
}

static VOID Bluetooth_Tip_Mbox_Create(lv_obj_t * par)
{
    lv_obj_t * mbox = lv_msgbox_create(par, NULL);
    lv_msgbox_set_text(mbox, (char *)lv_lang_get_text(PHONE_TEXT_ID_CONNECTED_ALREADY));
    lv_msgbox_set_anim_time(mbox, 0);
    lv_msgbox_start_auto_close(mbox, 2000);
}

static VOID Bluetooth_Update_Paired_Label_Show(VOID)
{
    if(Nav_Get_Top() != ACT_ID_BT)
       return;
    INT8 Str[20];
    if(bt_get_paired_dev_cnt() == 0)
        lv_label_set_text_id(gp_BtMainContList->ContList.ContainerList[2].Label.Label, PHONE_TEXT_ID_PAIRED_DEV);
    else {
        memset(Str, 0, 20);
        snprintf(Str, 20, "%s(%d)", (char *)lv_lang_get_text(PHONE_TEXT_ID_PAIRED_DEV), bt_get_paired_dev_cnt());
        lv_label_set_text(gp_BtMainContList->ContList.ContainerList[2].Label.Label, Str);
    }
}

static VOID Bluetooth_Open_State_Init(VOID)
{
    NV_UI_Bluetooth_t * Nvm = Bluetooth_Read_Nvm();
    printf("bt nvm on_off = %d bt_opened = %d\n", Nvm->on_off, bt_get_state());
    if(Nvm->on_off == 1) {
        bt_adp_task_create();
        bt_list_init();
        if(bt_get_state()) {
            Bluetooth_Show_Object(TRUE);
        } else {
            lv_obj_set_hidden(gp_OpeningLabel, FALSE);
            bt_open_device();
        }
    } else {
        if(bt_get_state()) {
            bt_adp_task_create();
            bt_list_init();
            Bluetooth_Show_Object(TRUE);
        }
    }
    Hal_Mem_Free(Nvm);
}

static VOID Bluetooth_Start_Scaning(VOID)
{
    if(Nav_Get_Top() != ACT_ID_BT) return;
    Bluetooth_Cancel_Inquiry();
    Bluetooth_Clean_Scan_List();
    Framework_Set_Suspend_Enable(FALSE);
    int8_t ret = bt_inquiry_device();
    if(ret == 0) {
        lv_obj_t * label = gp_BtMainContList->ContList.ContainerList[4].Label.Label;
        lv_obj_set_hidden(lv_obj_get_child(label, NULL), true);

        lv_obj_t * preload = lv_spinner_create(label, NULL);
        lv_obj_set_size(preload, 30, 30);
        lv_obj_set_style_local_bg_opa(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, LV_OPA_0);
        lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_WHITE);
        lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 2);
        lv_obj_set_style_local_line_rounded(preload, LV_SPINNER_PART_BG, LV_STATE_DEFAULT, 1);
        lv_obj_set_style_local_line_color(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x60,0xC0,0xFF));
        lv_obj_set_style_local_line_width(preload, LV_SPINNER_PART_INDIC, LV_STATE_DEFAULT, 3);
        lv_spinner_set_type(preload, LV_SPINNER_TYPE_SPINNING_ARC);
        lv_spinner_set_spin_time(preload, 3000);
        lv_obj_align(preload, NULL, LV_ALIGN_IN_RIGHT_MID, -5, 0);
        gp_ScaningPreload = preload;
        if(gp_BtMainContList->ContList.Index == 4)
        {
            lv_obj_t * Btnlabel = lv_obj_get_child(gp_BtMainContList->ButtonBar.ButtonM.Button, NULL);
            lv_label_set_text_id(Btnlabel, PHONE_TEXT_ID_STOP);
        }
    }
}

static VOID Bluetooth_Set_BtnBar_Text(VOID)
{
    lv_obj_t * sw = gp_BtMainContList->ContList.ContainerList[0].u.Sw.sw;
    lv_obj_t * label = lv_obj_get_child(gp_BtMainContList->ButtonBar.ButtonM.Button, NULL);
    if(gp_BtMainContList->ContList.Index == 0)
    {
        if(lv_switch_get_state(sw))
            lv_label_set_text_id(label, PHONE_TEXT_ID_CLOSE_BT);
        else
            lv_label_set_text_id(label, PHONE_TEXT_ID_OPEN_BT);
    }
    else if(gp_BtMainContList->ContList.Index == 1 && lv_switch_get_state(sw))
    {
        lv_label_set_text_id(label, PHONE_TEXT_ID_EDIT);
    }
    else if(gp_BtMainContList->ContList.Index == 4)
    {
        if(bt_get_inquiry_state() == BT_INQUIRY_INQUIRYING)
            lv_label_set_text_id(label, PHONE_TEXT_ID_STOP);
        else
            lv_label_set_text_id(label, PHONE_TEXT_ID_REFRESH);
    }
    else
    {
        lv_label_set_text_id(label, PHONE_TEXT_ID_SELECT);
    }
}

static VOID Bluetooth_MainUI_Update(VOID * pPara)
{
    UI_Normal_Container_List_Desc_t *NormalContainerListDesc = NULL;
    NormalContainerListDesc = (UI_Normal_Container_List_Desc_t *)pPara;

    lv_obj_t * sw = NormalContainerListDesc->ContList.ContainerList[0].u.Sw.sw;
    lv_obj_set_style_local_radius(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_GRAY);
    lv_obj_set_style_local_border_opa(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_shadow_width(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_top(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(sw, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, 0);

    lv_obj_set_style_local_radius(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_bg_opa(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x0B,0x85,0xFF));
    lv_obj_set_style_local_border_opa(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_shadow_width(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_top(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(sw, LV_SWITCH_PART_INDIC, LV_STATE_DEFAULT, 0);

    lv_obj_set_style_local_radius(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_bg_color(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_border_opa(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, LV_OPA_0);
    lv_obj_set_style_local_shadow_width(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, -2);
    lv_obj_set_style_local_pad_right(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, -2);
    lv_obj_set_style_local_pad_top(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, -2);
    lv_obj_set_style_local_pad_bottom(sw, LV_SWITCH_PART_KNOB, LV_STATE_DEFAULT, -2);

    INT8 Name[NV_BLUETOOTH_DEV_NAME_LEN];
    Bluetooth_Get_Local_Name(Name);
    printf("%s:name=%s\n", __FUNCTION__, Name);
    lv_label_set_text(NormalContainerListDesc->ContList.ContainerList[1].u.Label2.Label, Name);
    lv_obj_set_height(NormalContainerListDesc->ContList.ContainerList[0].Label.Label, lv_obj_get_height(NormalContainerListDesc->List) / 8);
    lv_obj_set_height(NormalContainerListDesc->ContList.ContainerList[2].Label.Label, lv_obj_get_height(NormalContainerListDesc->List) / 8);
    lv_obj_set_height(NormalContainerListDesc->ContList.ContainerList[3].Label.Label, lv_obj_get_height(NormalContainerListDesc->List) / 8);
    lv_obj_set_height(NormalContainerListDesc->ContList.ContainerList[4].Label.Label, lv_obj_get_height(NormalContainerListDesc->List) / 8);

    lv_obj_t * right_label1 = lv_label_create(NormalContainerListDesc->ContList.ContainerList[2].Label.Label, NULL);
    lv_label_set_text(right_label1, LV_SYMBOL_RIGHT);
    lv_obj_align(right_label1, NULL, LV_ALIGN_IN_RIGHT_MID, -5, 0);
    lv_obj_t * right_label2 = lv_label_create(NormalContainerListDesc->ContList.ContainerList[3].Label.Label, right_label1);
    lv_obj_t * refresh = lv_label_create(NormalContainerListDesc->ContList.ContainerList[4].Label.Label, right_label2);
    lv_label_set_text(refresh, LV_SYMBOL_REFRESH);
    lv_obj_align(refresh, NULL, LV_ALIGN_IN_RIGHT_MID, -5, 0);

    gp_OpeningLabel = lv_label_create(NormalContainerListDesc->List, NULL);
    lv_label_set_text_id(gp_OpeningLabel, PHONE_TEXT_ID_OPENING_BT);
    lv_obj_align(gp_OpeningLabel, NormalContainerListDesc->ContList.ContainerList[0].Label.Label, LV_ALIGN_OUT_BOTTOM_MID, 0, 30);
    lv_obj_set_hidden(gp_OpeningLabel, TRUE);
    Bluetooth_Set_BtnBar_Text();
}

static VOID Bluetooth_Get_Local_Name(INT8 * Name)
{
    NV_UI_Bluetooth_t * Nvm = Bluetooth_Read_Nvm();
    UINT8 Len = strlen(Nvm->local_name);
    if(Len > 0)
        Hal_Mem_Copy(Name, Nvm->local_name, NV_BLUETOOTH_DEV_NAME_LEN);
    else
        Hal_Mem_Copy(Name, "Phone\0", 6);
    Hal_Mem_Free(Nvm);
}

static VOID Bluetooth_Save_Local_Name(INT8 * Name, UINT8 Len)
{
    NV_UI_Bluetooth_t * Nvm = Bluetooth_Read_Nvm();
    Hal_Mem_Copy(Nvm->local_name, Name, Len);
    UI_NV_Write_Req(NV_SECTION_UI_BLUETOOTH, 0, sizeof(NV_UI_Bluetooth_t), (UINT8 *)Nvm);
    Hal_Mem_Free(Nvm);
}

static NV_UI_Bluetooth_t * Bluetooth_Read_Nvm(VOID)
{
    UINT32 Length = sizeof(NV_UI_Bluetooth_t);
    NV_UI_Bluetooth_t * Nvm = (NV_UI_Bluetooth_t *)Hal_Mem_Alloc(Length);
    if(Length != UI_NV_Read_Req(NV_SECTION_UI_BLUETOOTH, 0, Length, (UINT8 *)Nvm)) {
        printf("read bluetooth from nvm error\n");
        Hal_Mem_Free(Nvm);
        return NULL;
    }
    return Nvm;
}
#endif
